package com.example.sunsg.okhttptest.activity;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;

import com.breadtrip.R;
import com.example.sunsg.okhttptest.cuseomannotation.ViewAnnotation;
import com.example.sunsg.okhttptest.util.AnnotationUtil;
import com.example.sunsg.okhttptest.util.ScreenShotsUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class WebviewActivity extends AppCompatActivity implements View.OnClickListener{
    public static final String KEY_URL = "key_url";
    @ViewAnnotation(R.id.title)
    Button title;
    @ViewAnnotation(R.id.webview)
    WebView webview;

    private String mUrl;
    /**
     * 使用此webview 显示外部url页面
     */
    public static void startActivity(Context context, String url) {
        Intent intent = new Intent(context, WebviewActivity.class);
//        if (!(context instanceof Activity)) {
//            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//        }
        intent.putExtra(KEY_URL, url);
        context.startActivity(intent);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        WebView.enableSlowWholeDocumentDraw();//需要截图的调用此方法 此方法有显著的性能开销 在setContentView 之前调用
        setContentView(R.layout.activity_webview);
        AnnotationUtil.injectionView(this);
        handlerIntent();
        init();
        setUpWebView();
        loadUrl(mUrl);
    }

    private void init(){
        title.setOnClickListener(this);
    }


    private void handlerIntent(){
        if (getIntent() != null) {
            mUrl = getIntent().getStringExtra(KEY_URL);
        }
    }

    private void setUpWebView() {
        WebSettings ws = webview.getSettings();
        // 加上这句话才能使用javascript方法
        ws.setJavaScriptEnabled(true);
        ws.setLoadsImagesAutomatically(true);
        ws.setSupportZoom(true);
        ws.setSaveFormData(true);
        ws.setSavePassword(false);

        //ws.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        ws.setCacheMode(WebSettings.LOAD_NO_CACHE);

        //支持页面定位
        ws.setDatabaseEnabled(true);
        ws.setGeolocationEnabled(true);
        ws.setGeolocationDatabasePath(getApplicationContext().getFilesDir().getPath());
        //解决神州租车页面一直刷新的问题
        ws.setDomStorageEnabled(true);

        webview.setWebChromeClient(new WebChromeClient(){

        });
        webview.setWebViewClient(new WebViewClient(){

        });
    }

    private void loadUrl(String url){
        if(!TextUtils.isEmpty(url)){
            webview.loadUrl(mUrl);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.title:
                ScreenShotsUtil.screenShots(webview);
                break;
            case R.id.webview:
                break;
        }
    }
}
